package com.yixun.qfbao.hander;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.api.service.PersonnelService;
import com.yixun.qfbao.apidto.EmployeeDisabledReqDto;
import com.yixun.qfbao.apidto.EmployeeDisabledResDto;
import com.yixun.qfbao.apidto.ForbiddenOrNotPartnerReqDto;
import com.yixun.qfbao.apidto.ForbiddenOrNotPartnerResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.OtherSysUser;
import com.yixun.qfbao.model.dto.OtherSysUserDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.PartnerInfoEnum;
import com.yixun.qfbao.service.OtherSysUserService;
import com.yixun.qfbao.vo.EmployeeDisabledReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @program: channel-parent
 *
 * @description: V10员工离职
 *
 * @author: Seven.wang
 *
 * @create: 2019-12-02 10:56
 **/

@Service("employeeDisabledHander")
public class EmployeeDisabledHander extends CommonAbstractHander {

    private Logger logger = LoggerFactory.getLogger(EmployeeDisabledHander.class);

    @Autowired
    private PersonnelService personnelService;

    @Autowired
    private OtherSysUserService otherSysUserService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<EmployeeDisabledResDto> res = new BaseResponseDTO<EmployeeDisabledResDto>();
        EmployeeDisabledResDto resDto = new EmployeeDisabledResDto();
        EmployeeDisabledReqVo reqVo = (EmployeeDisabledReqVo)reqDto;
        EmployeeDisabledReqDto receiveIntentionReqDto = reqVo.getParams();
        logger.info("V10员工离职,获取消息推送信息 EmployeeDisabledReqVo info: {}", JSONObject.toJSONString(receiveIntentionReqDto));

        // 1.验证参数

        if(StringUtils.isEmpty(receiveIntentionReqDto.getEmployeeUuid())){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10经纪人id为空");
            return res;
        }
        if(StringUtils.isEmpty(receiveIntentionReqDto.getEmployeeStatus())){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10经纪人状态为空");
            return res;
        }
        if(!"离职".equals(receiveIntentionReqDto.getEmployeeStatus())){
            res.setResult(ResultEnum.SUCCESS.getNo());
            res.setErrorMsg("非离职状态!");
            return res;
        }

        if(StringUtils.isEmpty(receiveIntentionReqDto.getEmployeeName())){
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("v10经纪人姓名为空");
            return res;
        }
        try{
            OtherSysUserDto entity = new OtherSysUserDto();
            entity.setOtherCompanyId(receiveIntentionReqDto.getCompanyUuid());
            entity.setOtherUserId(receiveIntentionReqDto.getEmployeeUuid());
            entity.setIsDeleted(DelFlagEnum.UNDEL.getNo());
            OtherSysUser otherSysUser = otherSysUserService.selectByDto(entity);
            if(otherSysUser == null){
                resDto.setResult(ResultEnum.SUCCESS.getNo());
                resDto.setResultMsg("该经纪人未绑定渠道系统");
                res.setResult(resDto.getResult());
                res.setErrorMsg(resDto.getResultMsg());
                return res;
            }
            ForbiddenOrNotPartnerReqDto dto = new ForbiddenOrNotPartnerReqDto();
            dto.setId(otherSysUser.getChannelPartnerId());
            dto.setStatus(PartnerInfoEnum.STATUS_ENABLE.getCode());
            ForbiddenOrNotPartnerResDto forbiddenResDto = personnelService.forbiddenOrNotPartner(dto);
            if(!ResultEnum.SUCCESS.getNo().equals(forbiddenResDto.getResult())){
                res.setResult(forbiddenResDto.getResult());
                res.setErrorMsg(forbiddenResDto.getResultMsg());
                return res;
            }
            OtherSysUserDto otherDto = new OtherSysUserDto();
            otherDto.setIsDeleted(DelFlagEnum.DEL.getNo());
            otherDto.setId(otherSysUser.getId());
            otherSysUserService.update(otherDto);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
            resDto.setResultMsg("操作成功");
        }catch (Exception e){
            logger.error("EmployeeDisabledHander error: {}", e);
            resDto.setResult("4");
            resDto.setResultMsg(e.getMessage());
        }
        res.setResult(resDto.getResult());
        res.setErrorMsg(resDto.getResultMsg());
        res.setResData(resDto);
        return res;
    }

}
